﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Diagnostics;


namespace SampleVoiceForum
{
    public partial class recordMessage : System.Web.UI.Page
    {
        private String sSource = "YouTube Functions";
        private String sLog = "Application";
       
        protected void Page_Load(object sender, EventArgs e)
        {
            //Request.Files.Get(0).SaveAs("D:\\Uploads\\test.wav");
            if (!EventLog.SourceExists(sSource))
                EventLog.CreateEventSource(sSource, sLog);
         
        }

        public void AmplifySoundFile(String audioInputFileLocation, String programFolder)
        {
           
            String folderPath = Path.GetDirectoryName(audioInputFileLocation);
            String fileName = Path.GetFileName(audioInputFileLocation);
            String fileNameWithoutExtension = Path.GetFileNameWithoutExtension(audioInputFileLocation);

            String strCmdTxt = "/c cd " + programFolder + "\\sox & sox " + audioInputFileLocation + " -n stat -v";

            ProcessStartInfo i = new ProcessStartInfo("cmd.exe", strCmdTxt);
            i.CreateNoWindow = true;
            i.RedirectStandardOutput = true;
            i.RedirectStandardError = true;
            i.UseShellExecute = false;

            Process p = new Process();
            p.StartInfo = i;
            p.Start();

            var result = p.StandardOutput.ReadToEnd();
            String error = p.StandardError.ReadToEnd();

            bool exceptionOccured = false;

            double volume = 0;
            try
            {
                volume = Convert.ToDouble(error);
            }
            catch (FormatException formatException)
            {
                exceptionOccured = true;
                // Add the event logger
            }

            p.WaitForExit();
            EventLog.WriteEntry(sSource, "Volume permissible enhancement limit obtained", EventLogEntryType.Information);
            p.Close();

            if (!exceptionOccured)
            {
                strCmdTxt = "/c cd " + programFolder + "\\sox & sox " + audioInputFileLocation + " -r 8000 -t wav -c 1 " + folderPath + "\\" + fileNameWithoutExtension + "Temp.wav vol " + volume;
                //strCmdTxt = "/c cd C:\\ffmpeg & ffmpeg -i importantnew.wav -i image.jpg important.mkv";
                i = new ProcessStartInfo("cmd.exe", strCmdTxt);
                i.CreateNoWindow = true;
                i.RedirectStandardOutput = true;
                i.RedirectStandardError = true;
                i.UseShellExecute = false;

                p = new Process();
                p.StartInfo = i;
                p.Start();

                result = p.StandardOutput.ReadToEnd();
                error = p.StandardError.ReadToEnd();

                p.WaitForExit();
                EventLog.WriteEntry(sSource, "Volume amplified", EventLogEntryType.Information);
                p.Close();

                try
                {
                    System.IO.File.Replace(folderPath + "\\" + fileNameWithoutExtension + "Temp.wav", audioInputFileLocation,
                        folderPath + "\\" + fileNameWithoutExtension + "Backup.wav");
                    System.IO.File.Delete(fileNameWithoutExtension + "Temp.wav");
                    System.IO.File.Delete(fileNameWithoutExtension + "Backup.wav");

                    EventLog.WriteEntry(sSource, "Amplified files replaces the original audio files", EventLogEntryType.Information);

                }
                catch (Exception generalException)
                {
                    EventLog.WriteEntry(sSource, "Error in replacing the modified files:" + generalException.Message, EventLogEntryType.Error);
                }
            }

        }
    
    }
}